// pages/hole/hole_index/hole_index.js
const app = getApp()
const mta = require('../../../mta_sdk/mta_analysis.js');
Page({

  data: {
    bannerPath:'/images/hole_banner.jpg',
    active:1,
    allList:[],
    myList:[],
    showLogin:false,
    tempDataSet:{},
    isDelete:false,
    itemIndex:-1,
    zanNum:0,
    isZan:false,
    replayName:'',
    commentClick:false,
    showComment:false,
    commentVal:'',
    currPage_all:1,
    currPage_my:1,
    nodata_all:false,
    nodata_my:false,
    uid:'',
    ishome:false
  },

  onLoad: function (options) {
   
    this.setData({
      uid:wx.getStorageSync('userData').uid || ''
    });
    //this.refresh();
    mta.Page.init();
  },
  onShow(){
    this.refresh();
  },
  selectedTab(e){
    let index=e.currentTarget.dataset.tabindex;
    this.setData({
      active:index
    });
    /*
    if(!this.data.myList.length){
      this.getList(index)
    }
    if(this.data.isDelete){
      this.setData({
        isDelete:false,
        currPage_my:1
      });
      
    }*/
    this.refresh()
  },
  //获取列表
  getList(type){
    app.ajax({
      url: "Game/complain_list",
      loading:true,
      data:{
        type:type,
        page:type==1?this.data.currPage_all:this.data.currPage_my,
      }
    }).then(res => {
      
      if(type==1){
        this.setData({
          allList:this.data.currPage==1?res.data.list:[...this.data.allList,...res.data.list],
          nodata_all:res.data.list.length<10?true:false
        })
      }
      else{
        this.setData({
          myList:this.data.currPage==1?res.data.list:[...this.data.myList,...res.data.list],
          nodata_my:res.data.list.length<10?true:false
        })
      }
      wx.stopPullDownRefresh();
    });
  },
  getStart(e){
    let type=e.currentTarget.dataset.type
    app.userLogin(e.detail.userInfo,()=>{
      console.log('授权成功');
      if(type==1){
        wx.navigateTo({
          url: `/pages/hole/hole_release/hole_release`,
        })
      }
      //如果是点击赞
      if(type==2 && this.data.commentClick){
        let e={
          currentTarget:{
            dataset:this.data.tempDataSet
          }
        }
        this.addZan(e);
      }
      //如果是点击评论
      else{
        this.setData({
          showComment:true
        })
      }
      this.setData({
        showLogin:false
      });
    })
  },
  formSubmit(e){
    app.sendForm(e);
  },
  /*
  //点赞
  addZan(e){
    let md=this.data;
    let dataset=e.currentTarget.dataset;
    let[uid,itemid,index,iszan]=[dataset.uid,dataset.itemid,dataset.index,dataset.iszan];
    let item=md.active==1?md.allList[index]:md.myList[index];
   
    if(!wx.getStorageSync("userData").uid){
      this.setData({
        showLogin:true,
        tempDataSet:dataset
      });
      return false;
    }
    if(iszan==1){
      app.toast("已经赞过了~");
      return false;
    }
    app.ajax({
      url:"Game/complain_zan",
      loading:true,
      data:{
        uid:uid,
        id:itemid
      }
    }).then(res => {
      var intNums=parseInt(item.zan_num)+1;
      item.zan_num=intNums;
      item.zan=1;
      this.setList(md.allList,md.myList)
      
    });
  },
  setList(a,b){
    if(this.data.active==1){
      this.setData({
        allList:a
      });
    }
    else{
      this.setData({
        myList:b
      })
    }
  },
  //d点击评论弹出输入框
  showCommentInput(e){
    let dataset=e.currentTarget.dataset;
    let md=this.data;
    if(!wx.getStorageSync("userData").uid){
      this.setData({
        showLogin:true,
      })
    }
    else{
      this.setData({
        showComment:true,
        tempDataSet:dataset
      })
    }
  },
  bindKeyInput(e){
    this.setData({
      commentVal:e.detail.value
    })
  },
  inputOut(){
    if(!this.data.commentVal){
      this.setData({
        showComment:false
      })
    }
    
  },
  //提交评论
  subComment(){
    let val=this.data.commentVal;
    let sets=this.data.tempDataSet;
    let md=this.data;
    let[uid,itemid,index]=[sets.uid,sets.itemid,sets.index];
    let item=md.active==1?md.allList[index]:md.myList[index];
    if(val==''){
      app.toast('内容不能为空哦~');
    }
    else if(val.length<2){
      app.toast('内容太短啦。');
    }
    else{
      app.ajax({
        url:"Game/complain_comment_save",
        loading:true,
        loadTip:'提交中...',
        data:{
          uid:uid,
          id:itemid,
          type:0,
          comment:val
        }
      }).then(res => {
        app.toast('评论成功。');
        var intNums=parseInt(item.comment_num)+1;
        item.comment_num=intNums;
        this.setList(md.allList,md.myList)
        this.setData({
          showComment:false
        })
      })
    }
  },
  */
  //下拉刷新
  onPullDownRefresh: function(){
    this.refresh();
  },
  refresh(){
    let act=this.data.active;
    this.setData({
      currPage_all:act==1?1:this.data.currPage_all,
      currPage_my:act==2?1:this.data.currPage_my,
      allList:act==1?[]:this.data.allList,
      myList:act==2?[]:this.data.myList
    })
    this.getList(act);
  },
  //上啦加载
  onReachBottom(e){
    let td=this.data;
    if(td.active==1 && !td.nodata_all){
      this.setData({
        currPage_all:++td.currPage_all
      });
      this.getList(td.active);
    }
    if (td.active == 2 && !td.page) {
      this.setData({
        currPage_my:++td.currPage_my
      });
      this.getList(td.active);
    }
    console.log(td.currPage_all,td.currPage_my)
  },
  scrollBottom(e){
    
  },
  //删除
  removeItem(e){
    let dataset=e.currentTarget.dataset;
    let[uid,itemid]=[dataset.uid,dataset.itemid];
    wx.showModal({
      title: '温馨提示',
      content: '您确定要删除吗？',
      success: result => {
        if(result.confirm){
          app.ajax({
            url: "Game/complain_del",
            loading:true,
            data:{
              uid:uid,
              id:itemid
            }
          }).then(res => {
            app.toast('删除成功~');
            this.setData({
              isDelete:true,
              currPage_my:1
            });
            setTimeout(()=>{
              this.refresh()
            },1500);
            
          });
        }
      }
    });
  },
  //去详情页
  goInfo(e){
    wx.navigateTo({
      url:`/pages/hole/hole_info/hole_info?id=${e.currentTarget.dataset.id}`
    })
  },
  onShareAppMessage: function () {
  
  }
})